import { http } from "@/utils/http";

// 小区接口类型定义
export interface Cell {
  id: number; // 小区ID
  name: string; // 小区名称
  address?: string; // 小区地址
  description?: string; // 小区描述
  [key: string]: any;
}

// 楼栋接口类型定义
export interface Building {
  id: number; // 楼栋ID
  cellId: number; // 小区ID
  name: string; // 楼栋名称
  [key: string]: any;
}

// 单元接口类型定义
export interface Unit {
  id: number; // 单元ID
  buildingId: number; // 楼栋ID
  name: string; // 单元名称
  [key: string]: any;
}

// 房屋接口类型定义
export interface House {
  id: number; // 房屋ID
  unitId: number; // 单元ID
  name: string; // 房号
  [key: string]: any;
}

// 小区查询参数
export interface CellQueryParams {
  page?: number;
  size?: number;
  name?: string;
  [key: string]: any;
}

// 通用响应接口
export interface ApiResponse<T = any> {
  code: number;
  data?: T;
  message: string;
}

// 小区列表响应
export interface CellListResponse
  extends ApiResponse<{
    data: Cell[];
    count: number;
  }> {}

// 更新小区
export const updateCell = (data?: Cell) => {
  return http.request<ApiResponse>("put", "/spCell/update", { data });
};

// 创建小区
export const createCell = (data?: Cell) => {
  return http.request<ApiResponse>("post", "/spCell/create", { data });
};

// 查询小区下的房屋列表
export const searchHouseList = (unitId: number) => {
  return http.request<ApiResponse<House[]>>(
    "get",
    `/spCell/searchHouseList/${unitId}`
  );
};

// 查询小区下的单元列表
export const searchUnitList = (buildingId: number) => {
  return http.request<ApiResponse<Unit[]>>(
    "get",
    `/spCell/searchUnitList/${buildingId}`
  );
};

// 查询小区下的楼栋列表
export const searchBuildingList = (cellId: number) => {
  return http.request<ApiResponse<Building[]>>(
    "get",
    `/spCell/searchBuildingList/${cellId}`
  );
};

// 通过ID查询小区
export const findCellById = (id: number) => {
  return http.request<ApiResponse<Cell>>("get", `/spCell/findById?id=${id}`);
};

// 查询所有小区
export const findAllCells = () => {
  return http.request<ApiResponse<Cell[]>>("get", "/spCell/findAll");
};

// 删除小区
export const deleteCell = (id: number) => {
  return http.request<ApiResponse>("delete", `/spCell/delete/${id}`);
};
